<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	//     public function index(){
	//     	$user_course = M('user_course');
	//         $id = I('post.id');
	//       // $data = M('user_course')->where(array('dou_user_course.id'=>$id))->join('dou_course_list on dou_course_list.id = dou_user_course.course_list_id')->find();
	//  //var_dump($data);
	// 		$now_time= date("m-d H:i",strtotime('+2 hour'));
	// 		//$where['start_time']=array('eq',$now_time);
	// 		//$where['id']=array('dou_course_list.id'=>$id);
	// 		//$openid = M('user')->where(array('id'=>is_login()))->getfield('openid');

	// 		$course_data = M('course_list')->join("dou_course on dou_course.id = dou_course_list.course_id")->where($where)->find();
	// 		dump($course_data);
	// 		//$now= date("m-d H:i",);		
	// 		$bb = date("m-d H:i",$course_data['start_time']);
	// 		echo $bb;
	// 		//$cc = date("m-d H:i",$bb);
	// }

	 public function index(){
	 		$course=M('course_list');
			$where['start_time']=array('between',array(strtotime('-1 hour'),strtotime('+1 hour')));
			$where['dou_user_course.is_remind']=0;
			$info=$course->where($where)->join('dou_course on dou_course_list.course_id = dou_course.id')->join('dou_user_course on dou_course_list.id = dou_user_course.course_list_id')->select();
			var_dump($info);
			var_dump($where);
			foreach ($info as $key => $value) {
				//$this->template_message();  判断发送模板消息
				$user=M('user_course');
				$data['is_remind']=1;
				$where['course_list_id']=$info[$key]['course_list_id'];
				$where['user_id']=$info[$key]['user_id'];
				$result=$user->where($where)->save($data);
				var_dump($result);
			}
	 }

	 public function getWxAccessToken(){
		$appid='wx2eba7aba580e31af';
		$appsecret='0c5b6572fecaa37bf292d76d174bff11';
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
		$res=$this->https_post($url);
		$jsoninfo=json_decode($res,true);
		$access_token=$jsoninfo['access_token'];
		return $access_token;
	}

	public function template_message(){
		$access_token=$this->getWxAccessToken();
		$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
		$send_message='{
							   "touser":"ox0yDwoJD500KL5AAS3hbIqmPkMk",
							   "template_id":"CErXn6kzkhUaaE6R9NDcbWtdyTDdluxbOxp-oMfBp6c",
							   "url":"http://dadouyundong.com/",            
							   "data":{
										"title": {
										   "value":"你成功取消本次训练。\n",
										   "color":"#000000"
									   },
										"content": {
										   "value":"555",
										   "color":"#173177"
									   },
									   "time": {
										   "value":"vbvf",
										   "color":"#173177"
									   }
									   }
								   }';
		$send_arr=$this->https_post($url,$send_message);
		$jsoninfo=json_decode($send_arr,true);
		var_dump($jsoninfo);

	}

	public function https_post($url,$data=null){
		$curl=curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		if(!empty($data)){
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output=curl_exec($curl);
		curl_close($curl);
		return $output;

	}

}